﻿using System;
using System.Web.UI;
using olierFrm.DataBase.FrameWork.StdQuery;
using olierFrm.WebFormDev.Plugin;
using olierFrm.WebFormDev.StdQuery;
using olierFrm.WebFormDev;
using olierFrm.WebFormDev.Session;
using olierFrm.Extensions.EDataTable;

namespace WebFormDev.StdQueryPack
{
    public partial class IFrameStdQuery : System.Web.UI.Page
    {

        protected void Page_PreInit(object sender, EventArgs e)
        {
            ManageSystem.IsLogin();
            ManageSystem.SetTheme(Page);
            //没有QueryName只显示菜单，为了重复利用该页面（系统首页和StdQuery）

        }

        private string QueryName = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            #region 处理模块，统一模块、标准查询
            //标准查询

            var dbStdQueryPower = new StdQuerySQL("GetModulePower");
            dbStdQueryPower.AddSystemParams();
            dbStdQueryPower.SetParam("ModuleID", StdQuery.ModuleID);
            var dtModulePower = dbStdQueryPower.ExecuteSQL();

            //可进入模块
            var enterModulePower = dtModulePower.SelectToDataTable(" PowerID = '" + StdQuery.ModuleID + "'");
            if (enterModulePower.Rows.Count == 0)
            {
                panelStdQueryHeader.Visible = false;
                Response.Clear();
                Response.Write("没有进入该模块的权限，请联系管理员！");
                return;
            }
            //可预览
            //var previewModulePower = dtModulePower.SelectToDataTable(" PowerID = '" + StdQuery.ModuleID + "_1'");
            //可导出Excel
            var excelModulePower = dtModulePower.SelectToDataTable(" PowerID = '" + StdQuery.ModuleID + "_2'");
            if (excelModulePower.Rows.Count == 0)
            {
                btnExport.Visible = false;
            }


            if (Request.QueryString["ModuleType"] != null && Request.QueryString["ModuleType"] == "0")
            {
                QueryName = StdQuery.QueryName;
            }
            //统一模块
            else if (Request.QueryString["ModuleType"] != null && Request.QueryString["ModuleType"] == "1")
            {

                ReceiptsConfigEntity rce = new ReceiptsConfigEntity(StdQuery.ModuleID);
                form1.Attributes.Add("moduletype", Request.QueryString["ModuleType"]);
                QueryName = rce.StdQueryEntity.QueryName;
                /*
                if (rce.BusinessEntity.Add != null)
                {
                    var addModulePower = dtModulePower.SelectToDataTable(" PowerID = '" + rce.BusinessEntity.Add + "' ");
                    if (addModulePower.Rows.Count != 0)
                    {
                        btnAdd.Visible = true;
                        form1.Attributes.Add("moduleid", "Unified_" + StdQuery.ModuleID);
                        form1.Attributes.Add("modulename", rce.ModuleInfoEntity.ModuleName);
                        form1.Attributes.Add("modifyurl", "../DataObjectPack/IFrameDataObject.aspx?ModuleID=" + StdQuery.ModuleID + "&DoAction=Add");
                        //var url = "../DataObjectPack/IFrameDataObject.aspx?ModuleID=" + rc.Config.ModuleID;
                        //btnAdd.ClientSideEvents.Click = "function(s,e){OpenModule('" + rc.Config.ModuleName + "','" + url + "','Unified_" + rc.Config.ModuleID + "');e.processOnServer = false;}";
                    }
                }
                if (rce.BusinessEntity.Modify != null)
                {
                    var modifyModulePower = dtModulePower.SelectToDataTable(" PowerID = '" + rce.BusinessEntity.Modify + "' ");
                    if (modifyModulePower.Rows.Count != 0)
                    {
                        //DBFrm.FrameWork.DataObject.DataObjectSQL dos = new DBFrm.FrameWork.DataObject.DataObjectSQL(rc.Config.ObjectName);
                        //foreach (var p in dos.DataObjectParams)
                        //{

                        //}
                        //b.BusinessParams.
                    }
                }*/
            }
            #endregion



            hfCurQueryName.Value = QueryName;

            StdQuerySQL dbStdQuery = new StdQuerySQL(QueryName);
            var controls = StdQueryParam.StdQueryParamControls(dbStdQuery.StdQueryParams);
            foreach (var c in controls)
            {
                panelStdQueryParam.Controls.Add(c);
                try
                {
                    HandelControl.SetControlValue(c.Controls[0].Controls[0].Controls[0].Controls[1], Request.QueryString[c.ID.Replace("Field_", "")]);
                }
                catch { }
            }

            panelStdQueryParam.Controls.Add(new LiteralControl("<div style='clear:both'></div>"));
            if (Request.QueryString["AE"] == "1")
            {
                splitStdQueryContent.Controls.Add(new LiteralControl("<iframe id='IFrameStdQueryContent' frameborder='0' height='100%' width='100%' scrolling='no' src='IFrameStdQueryContent.aspx" + Request.Url.Query + "&hfThisWidth=" + hfThisWidth.Value + "' scrolling='auto'></iframe>"));
            }
            //自动查询
            else if ((dbStdQuery.StdQuery.Property & 1) == 1)
            {
                HandelURL hu = new HandelURL("");
                hu.QueryString.Add("QueryName", StdQuery.QueryName);
                hu.QueryString.Add("ModuleID", StdQuery.ModuleID);
                foreach (var p in dbStdQuery.StdQueryParams)
                {
                    var control = panelStdQueryParam.FindControl("Field_" + p.ParamName);
                    if (control == null) continue;
                    string value = HandelControl.GetControlValue(control.FindControl(p.ParamName)).ToString();
                    hu.QueryString.Add(p.ParamName, value);
                }
                splitStdQueryContent.Controls.Add(new LiteralControl("<iframe id='IFrameStdQueryContent' frameborder='0' height='100%' width='100%' scrolling='no' src='IFrameStdQueryContent.aspx?hfThisWidth=" + hfThisWidth.Value + "&" + hu.ToString() + "' scrolling='auto'></iframe>"));
            }


            return;
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                SGrid.Clear(QueryName);
            }
            catch { }
            HandelURL hu = new HandelURL("");
            hu.QueryString.Add("QueryName", QueryName);
            if (Request.QueryString["RK"] != null) hu.QueryString.Add("RK", Request.QueryString["RK"]);
            if (Request.QueryString["ModuleID"] != null) hu.QueryString.Add("ModuleID", Request.QueryString["ModuleID"]);
            //if (Request.QueryString["AE"] == null)
            hu.QueryString.Add("AE", "1");
            var dbStdQuery = new StdQuerySQL(QueryName);
            foreach (var p in dbStdQuery.StdQueryParams)
            {
                var control = panelStdQueryParam.FindControl("Field_" + p.ParamName);
                if (control == null) continue;

                string value = "";
                var controlValue = HandelControl.GetControlValue(control.FindControl(p.ParamName));
                if (controlValue != null)
                {
                    value = controlValue.ToString();
                }
                hu.QueryString.Add(p.ParamName, value);
            }


            Response.Redirect(Request.Url.LocalPath + "?" + hu.ToString());
        }
    }
}